using System;
using System.Text;
using System.Collections.Generic;
using Common.Core.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Common.Core.Testing
{
	/// <summary>
	/// Summary description for UnitTest1
	/// </summary>
	[TestClass]
	public class ConfigurationSettingsNameAttributeTests
	{
		#region Additional test attributes
		//
		// You can use the following additional attributes as you write your tests:
		//
		// Use ClassInitialize to run code before running the first test in the class
		// [ClassInitialize()]
		// public static void MyClassInitialize(TestContext testContext) { }
		//
		// Use ClassCleanup to run code after all tests in a class have run
		// [ClassCleanup()]
		// public static void MyClassCleanup() { }
		//
		// Use TestInitialize to run code before running each test 
		// [TestInitialize()]
		// public void MyTestInitialize() { }
		//
		// Use TestCleanup to run code after each test has run
		// [TestCleanup()]
		// public void MyTestCleanup() { }
		//
		#endregion

		[TestMethod]
		public void RetrieveByAttribute()
		{
			Settings settings = ConfigurationSettings.Retrieve<Settings>( ConfigurationSourceFactory.Create() );
			Assert.IsNotNull( settings );
			Assert.AreEqual( "Hello", settings.TestProperty );
		}

		[TestMethod]
		public void RetrieveByName()
		{
			Settings settings = ConfigurationSettings.Retrieve<Settings>( "settings", ConfigurationSourceFactory.Create() );
			Assert.IsNotNull( settings );
			Assert.AreEqual( "Hello", settings.TestProperty );
		}

		[TestMethod]
		public void RetrieveByInvalidNameIsNull()
		{
			Settings settings = ConfigurationSettings.Retrieve<Settings>( "hehe", ConfigurationSourceFactory.Create() );
			Assert.IsNull( settings );
		}

		[TestMethod]
		[ExpectedException(typeof(ArgumentNullException))]
		public void RetrieveByNoNameThrows()
		{
			ConfigurationSettings.Retrieve<PlainSettings>( ConfigurationSourceFactory.Create() );
		}

		[TestMethod]
		public void RetrievePlainSettingsByName()
		{
			PlainSettings settings = ConfigurationSettings.Retrieve<PlainSettings>( PlainSettings.Name, ConfigurationSourceFactory.Create() );
			Assert.IsNotNull( settings );
			Assert.AreEqual( "Dolly", settings.TestProperty );
		}
	}
}
